﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LitJson;

namespace CoolChat
{
    public partial class Send : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NewMethod();
        }
        private void NewMethod()
        {
            //注册DateTime类型如何转换为字符串
            JsonMapper.RegisterExporter(new ExporterFunc<DateTime>(Tools.DateTimeExForJson));
            Lang lang = new Lang();
            string act = Request["act"];//操作: send 发送一条消息, rev 接收消息, L 登陆
            string msg = Request["msg"];//发送消息时传入
            string ul = Request["ul"];//ul=1 返回在线用户列表, ul=0 返回不在线用户列表,ul=2 返回系统用户和用户列表(不管系统用户是否在线)
            string rjson = "";
            MsgReturn mreturn = new MsgReturn();
            if (string.IsNullOrEmpty(act) && string.IsNullOrEmpty(ul))
            {
                mreturn.Act = new KeyValuePair<ClientAct, string>[1] { new KeyValuePair<ClientAct, string>(ClientAct.ERROR, "we dont know what to do.there's no act") };
                rjson = JsonMapper.ToJson(mreturn);
                Response.Write(rjson);
                return;
            }
            ChatUser u = Tools.getCurUser();
            switch (act)
            {//接收,发送或者登陆
                case "send":
                    ChatMessage cm = null;
                    try
                    {
                        msg.Replace("\"SilenceExpires\":null", "\"SilenceExpires\":\"2013-11-18 00:00:00\"");
                        cm = JsonMapper.ToObject<ChatMessage>(msg);
                    }
                    catch (JsonException jex)
                    {
                        mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED, "object pass error" +"<br/>"+msg+"<br/>"+jex.ToString()) };
                        break;
                    }
                    try
                    {
                        if (u == null)
                        {
                            mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED,lang["ZH_CN"]["RELOGIN"].ToString()) };
                            break;
                        }
                        if (Dbo.UpdateUserLoginState(u) == 0)
                        {//已经被追踪用户在线的线程标记为下线并且删除了...提示用户重新登陆吧..
                            Tools.setCurUser(null);
                            mreturn.Act = new KeyValuePair<ClientAct, string>[]{
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED, lang["ZH_CN"]["RELOGIN"].ToString())
                            };
                            break;
                        }
                        cm.From = u;
                        u.SilenceExpires = Dbo.GetUserSilence(u.UserID);
                        if (DateTime.Now < u.SilenceExpires)
                        {//用户被禁言
                            mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED,"") };
                            mreturn.Silence = true;
                            break;
                        }
                        //mreturn.Silence = false;
                        if (string.IsNullOrEmpty(cm.MsgID))
                        {
                            mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED,"msg id is null or empty") };
                            break;
                        }
                        if (cm.To == null)
                        {
                            cm.To = new ChatUser();
                            cm.To.UserID = Tools.md5(cm.MsgID + DateTime.Now, 16);
                            cm.To.UserName = "*";//* 代表所有人//lang["ZH_CN"]["ALL_BODY"].ToString();
                            cm.To.UserIP = "*";
                        }
                        cm.From.UserIP = Request.UserHostAddress;
                        int r = Dbo.AddMessage(cm);
                        if (r > 0)
                        {
                            mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_SUCCESS,JsonMapper.ToJson(cm)) };
                        }
                        else
                        {
                            mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                                new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED,JsonMapper.ToJson(cm)) };
                        }
                    }
                    catch (Exception ex)
                    {
                        mreturn.Act = new KeyValuePair<ClientAct, string>[1] { 
                            new KeyValuePair<ClientAct, string>(CoolChat.ClientAct.MSG_FAILED,ex.ToString()) };
                    }
                    break;
            } 
            rjson = JsonMapper.ToJson(mreturn);
            Response.Write(rjson);//输出处理结果
            mreturn = null;
        }
    }
}